// pages/home-page/project/project.js
const util = require('../../../utils/util.js')
const { baseUrl } = require('../../../utils/baseUrl.js');
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // url: 'https://xsfaappuat.cpic.com.cn/planbook/newPlanbook/index.html#/productDetail',
    url: `https://${baseUrl}/h5/#/`,
    outerUrl: '',
    shareId: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad (options) {
    const { customerId, productId, productCode, shareId = '', check = false } = options
    this.setData({
      customerId,
      productId,
      productCode,
      shareId
    })
    const workNum = `YB_${app.globalData.userInfo.workNum}`
    if (check) {
      // 现场演示利益演示跳转,只需要计划书详情
      const url = await this.getShareURL()
      this.setData({ url })
    } else if (customerId) {
      // 查看/生成计划书,有shareId表示查看计划书
      if (!shareId) {
        wx.setNavigationBarTitle({
          title: '生成计划书'
        })
      }
      this.setData({
        url: `${this.data.url}proposal?productCode=${productCode}&from=YB&empNo=${workNum}&customerId=${customerId}&productId=${productId}&session=${wx.getStorageSync("token")}&agentName=${app.globalData.userInfo.realName}&shareId=${shareId}`,
      })
    } else {
      // 个人名片-查看计划书
      this.setData({
        url: `${this.data.url}effectInsurance?productCode=${productCode}&from=YB&empNo=${workNum}&productId=${productId}&orgId=${app.globalData.userInfo.orgId}&session=${wx.getStorageSync("token")}`,
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  async onShareAppMessage (options) {
    let { outerUrl, shareId } = this.data
    if (!outerUrl) {
      const url = await this.getShareURL()
      outerUrl = url
      shareId = this.data.shareId
      this.setData({ outerUrl })
    }
    return {
      title: '计划书',
      path:  `/pages/share-proposal/shareProposal?url=${encodeURIComponent(outerUrl)}&manager=${app.globalData.userInfo.realName}&shareId=${shareId}&userId=${app.globalData.userInfo.userId}`
    }
  },
  async getShareURL () {
    const { customerId, productId } = this.data
    console.log(111)
    return app.api.post({
      url: 'customer_stage_getShareURL',
      data: {
        customerId,
        productId,
      }
    }).then(res => {
      const userCode = `"${res.userCode}"`
      const url = `${res.shareUrl}shareid=${res.shareId}&paramId=${res.paramId}&source=${res.source}&productSource=${res.productSource}&productCode=${res.productCode}&userCode=${util.encode(userCode)}`
      this.setData({
        outerUrl: url,
        shareId: res.shareId
      })
      console.log(`url=${url}&manager=${app.globalData.userInfo.realName}&shareId=${res.shareId}`)
      return url
    })
  },
  train: function (params) {
    const { customerId, productId, productCode } = this.data
    app.api.post({
      url: 'customer_stage_add',
      data: {
        customerId,
        productId,
        stage: 'appointment' // appointment 约访训练，interview 面谈训练 
      }
    }).then(res => {
      wx.navigateTo({
        url: `/pages/home-page/appointment/appointment?customerId=${customerId}&productId=${productId}&productCode=${productCode}`
      })
    })
  },
  intervalGetImg () {
    const intervalId = setInterval(() => {
      app.api.post({
        url: 'customer_stage_getPlanResource',
        data: {
          customerId,
          productId
        }
      }).then(res => {
        if (res.data){
          clearInterval(this.data.intervalId)
          this.setData({
            imgUrl: res.data,
            webViewShow: false
          })
        }
      })
    }, 1000);
    this.setData({
      intervalId
    })
  },
})